<?php

class Edit extends TPage
{
	public $setting_manager;
	
	public $conference_name_setting;
	public $mail_setting;
	public $mail_username_setting;
	public $mail_password_setting;
	public $mail_server_setting;
	public $early_registration_date_setting;
	public $currency_name_setting;
	
	public $settings = array();
	
	public function __construct()
	{
		parent::__construct();
		$this->settings = array(
						SettingManager::$CONFERENCE_NAME =>
								array(	'record_name' => 'conference_name_setting',
										'field_name' => 'conference_name_field'),
						SettingManager::$MAIL =>
								array(	'record_name' => 'mail_setting',
										'field_name' => 'mail_field'),
						SettingManager::$MAIL_USERNAME =>
								array(	'record_name' => 'mail_username_setting',
										'field_name' => 'mail_username_field'),
						SettingManager::$MAIL_PASSWORD =>
								array(	'record_name' => 'mail_password_setting',
										'field_name' => 'mail_password_field'),
						SettingManager::$MAIL_SERVER =>
								array(	'record_name' => 'mail_server_setting',
										'field_name' => 'mail_server_field'),
						//SettingManager::$EARLY_REGISTRATION_DATE =>
						//		array(	'record_name' => 'early_registration_date_setting',
						//				'field_name' => 'early_registration_date_field'),
						SettingManager::$CURRENCY_NAME =>
								array(	'record_name' => 'currency_name_setting',
										'field_name' => 'currency_name_field')
					);
	}
						
	public function onLoad($param)
	{
		parent::onLoad($param);
		$this->setting_manager = $this->Application->Modules['settings'];

		foreach(array_keys($this->settings) as $key)
		{
			$record_name = $this->settings[$key]['record_name'];
			$this->$record_name = $this->setting_manager->getSettingByName($key);
		}
		$this->early_registration_date_setting = $this->setting_manager->getSettingByName(SettingManager::$EARLY_REGISTRATION_DATE);
		
		if(!$this->IsPostback)
			$this->readData();
	}
	
	public function readData()
	{
		foreach(array_keys($this->settings) as $key)
		{
			$record_name = $this->settings[$key]['record_name'];
			$field_name = $this->settings[$key]['field_name'];
			$this->$field_name->Text = $this->$record_name->getValue();
		}
		
		if($this->early_registration_date_setting->getValue() == null)
			$this->early_registration_date_checkbox->setChecked(false);
		else
		{
			$this->early_registration_date_checkbox->setChecked(true);
			$this->early_registration_date_field->setDate($this->early_registration_date_setting->getValue());
		}
	}
	
	public function saveButtonClicked($sender, $params)
	{
		if($this->Page->IsValid)
		{
			$result = true;
			foreach(array_keys($this->settings) as $key)
			{
				$record_name = $this->settings[$key]['record_name'];
				$field_name = $this->settings[$key]['field_name'];
				
				$this->$record_name->setValue($this->$field_name->Text);
				if($this->setting_manager->updateSettingBySettingRecord($this->$record_name) == null)
					$result = false;
			}
			
			if($this->early_registration_date_checkbox->getChecked())
				$this->early_registration_date_setting->setValue($this->early_registration_date_field->getDate());
			else
				$this->early_registration_date_setting->setValue(null);
			
			if($this->setting_manager->updateSettingBySettingRecord($this->early_registration_date_setting) == null)
				$result = false;
			
			if($result)
				$this->info->Text = '<p>'.Prado::localize('Changes saved').'</p>';
			else
				$this->info->Text = '<p>'.Prado::localize('Changes not saved').'</p>';
		}
	}
}

?>